package cn.wolfcode.luowowo.web.controller;

import cn.wolfcode.luowowo.annotion.RequireLogin;
import cn.wolfcode.luowowo.annotion.UserParam;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelsService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.management.ObjectName;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelsController {
    @Autowired
    private ITravelsService travelsService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private ITravelStatisVORedisService travelStatisVORedisService;
    @GetMapping("/query")
    public Object query(TravelQuery qo,HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        //通过token获取当前登录用户,以此来判断是否登录
        UserInfo user = userInfoRedisService.getUserByToken(token);
        return JsonResult.success(travelsService.query(qo,user));
    }
    @RequireLogin //登录
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        //通过token获取当前登录用户,以此来判断是否登录
        UserInfo user = userInfoRedisService.getUserByToken(token);
        travel.setUserId(user.getId());
        travelsService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }

    @GetMapping("/input")
    public Object input(String id) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.hasLength(id)) {
            Travel travel = travelsService.get(id);
            map.put("tv", travel);
        }
        List<Destination> dests = destinationService.list();
        map.put("dests", dests);
        return JsonResult.success(map);
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
    @GetMapping("/detail")
    public Object detail(String id){
        //在每次访问游记内容时,都会让阅读数+1
        travelStatisVORedisService.addViewnum(id,1);
        return JsonResult.success(travelsService.get(id));
    }
    //添加游记评论操作
    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo){
        //把userInfo对象中的参数拷贝到travelComment对象中
        BeanUtils.copyProperties(userInfo,travelComment);
        //设置游记评论的发布人id
        travelComment.setUserId(userInfo.getId());
        //再进行保存操作
        travelCommentService.save(travelComment);

        //评论+1
        travelStatisVORedisService.addReplynum(travelComment.getTravelId());

        //最后返回一个成功的结果即可
        return JsonResult.success();
    }
    //显示游记评论操作
    @GetMapping("/comments")
    public Object comments(String travelId){
        //获取当前游记中的所有评论
        List<TravelComment> comments = travelCommentService.queryByTravelId(travelId);
        //再返回回去
        return JsonResult.success(comments);
    }
    //显示点击量前五的游记
    @GetMapping("getViewnum5")
    public Object getViewnum5(){
        return JsonResult.success(travelsService.getViewnum5());
    }
    //查询最新的10条游记评论
    @GetMapping("getNewComments10")
    public Object getNewComments10(){
        return JsonResult.success(travelCommentService.getNewComments10());
    }
    //顶顶功能
    @RequireLogin
    @PostMapping("/travelThumbup")
    public Object travelThumbup(String tid,@UserParam UserInfo userInfo){
        //true-->顶顶成功   false-->今天已经顶过了
        boolean ret = travelStatisVORedisService.travelThumbup(tid,userInfo.getId());
        return JsonResult.success(ret);
    }
    /**
     * 数据回显
     * @param tid
     * @return
     */
    @GetMapping("statisVo")
    public Object statisVo(String tid){
        return JsonResult.success(travelStatisVORedisService.getTravelStatisVo(tid));
    }
    //游记收藏
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String tid,@UserParam UserInfo userInfo){
        //true-->收藏成功   false-->取消收藏
        boolean ret = travelStatisVORedisService.favor(tid,userInfo.getId());
        return JsonResult.success(ret);
    }
}
